/* eslint-disable no-undef */
export function bxjVerify() {
  // 自定义验证函数(str:输入值, boolean:是否为提交校验)
  const validateEmail = (str, boolean) => {
    if (boolean && str === "") {
      return true;
    }
    if (str === "") return;
    const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    if (!emailPattern.test(str)) {
      return true;
    } else {
      return false;
    }
  };
  const validateField = (str, boolean) => {
    if (boolean && str === "") {
      return true;
    }
    if (str === "") return;
    if (str.length < 3) {
      return true;
    } else {
      return false;
    }
  };
  const validateCode = (str, boolean) => {
    if (boolean && str === "") {
      return true;
    }
    if (str === "") return;
    if (str.length < 3) {
      return true;
    } else {
      return false;
    }
  };
  const endVerify = (inputRefs, inputs, form) => {
    let flag = false;
    inputRefs.value.forEach((inputRef, index) => {
      if (inputRef) {
        // 获取当前字段的最新值
        const fieldName = inputs.value[index].name; // 获取字段名的小写形式
        const fieldValue = form.value[fieldName]; // 通过索引 form 获取字段的值
        inputRef.triggerBlur(fieldValue, true);
        if (inputRef.fail) flag = true;
      }
    });
    if (flag) return true;
  };
  return {
    validateEmail,
    validateField,
    validateCode,
    endVerify,
  };
}
